<template>
  <div class="warehouse-manage-page">
    <el-card style="max-width: 1200px; margin: 0 auto;">
      <div class="toolbar">
        <el-button @click="$router.back()">返回</el-button>
        <el-button type="primary" @click="onAdd">新增仓库</el-button>
      </div>
      <el-form v-if="formVisible" :model="form" label-width="70px" style="max-width: 400px; margin-bottom: 24px;">
        <el-form-item label="仓库名称" required>
          <el-input v-model="form.name" maxlength="10" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.desc" maxlength="30" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="form.sort" :min="1" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSave">保存</el-button>
          <el-button @click="onCancel">取消</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="warehouseList" border size="small" style="margin-bottom: 0;">
        <el-table-column prop="name" label="仓库名称" />
        <el-table-column prop="desc" label="描述" />
        <el-table-column prop="sort" label="排序" width="80" />
        <el-table-column label="操作" width="140">
          <template #default="scope">
            <div style="display: flex; gap: 8px; justify-content: flex-start; padding-left: 8px;">
              <el-button size="small" @click="onEdit(scope.row)">编辑</el-button>
              <el-button size="small" type="danger" @click="onDelete(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
// 假设locationList为全局/父组件/Pinia数据，这里用mock
const locationList = ref([
  { id: 1, warehouse: 1, stock: 60 },
  { id: 2, warehouse: 1, stock: 80 },
  { id: 3, warehouse: 2, stock: 30 }
])
function canDeleteWarehouse(warehouseId: number) {
  return !locationList.value.some(l => l.warehouse === warehouseId && l.stock > 0)
}
const router = useRouter()
const warehouseList = ref([
  { id: 1, name: '上海仓', desc: '华东主仓', sort: 1 },
  { id: 2, name: '北京仓', desc: '华北主仓', sort: 2 }
])
const formVisible = ref(false)
const isEdit = ref(false)
const form = ref({ id: 0, name: '', desc: '', sort: 1 })
function onAdd() {
  form.value = { id: 0, name: '', desc: '', sort: 1 }
  isEdit.value = false
  formVisible.value = true
}
function onEdit(row: any) {
  form.value = { ...row }
  isEdit.value = true
  formVisible.value = true
}
function onSave() {
  if (!form.value.name) return ElMessage.warning('请输入仓库名称')
  // 重名校验
  const isDuplicate = warehouseList.value.some(w => w.name === form.value.name && (isEdit.value ? w.id !== form.value.id : true))
  if (isDuplicate) return ElMessage.error('仓库名称已存在，不允许重名！')
  if (isEdit.value) {
    const idx = warehouseList.value.findIndex(t => t.id === form.value.id)
    if (idx > -1) warehouseList.value[idx] = { ...form.value }
    ElMessage.success('仓库修改成功')
  } else {
    warehouseList.value.push({
      id: Date.now(),
      name: form.value.name,
      desc: form.value.desc,
      sort: form.value.sort
    })
    ElMessage.success('仓库新增成功')
  }
  formVisible.value = false
}
function onCancel() {
  formVisible.value = false
}
function onDelete(row: any) {
  if (!canDeleteWarehouse(row.id)) {
    ElMessage.error('该仓库下存在商品库存，无法删除！')
    return
  }
  ElMessageBox.confirm('确定要删除该仓库吗？', '提示', { type: 'warning' })
    .then(() => {
      warehouseList.value = warehouseList.value.filter(t => t.id !== row.id)
      ElMessage.success('删除成功')
    })
}
</script>

<style scoped>
.warehouse-manage-page { padding: 24px; background: #f7f8fa; min-height: 100vh; }
.toolbar { display: flex; gap: 12px; margin-bottom: 24px; align-items: center; }
</style> 